import { load, request } from '../common_node.js'
import {testContent, testSearch} from "../crawler.test.js";

const source = {
  "id": "free_book_js/8",
  "name": "新笔趣阁",
  "portalUrl": "https://www.xbiqugeo.com",
  "searchBaseUrl": "https://www.xbiqugeo.com",
  "bookBaseUrl": "https://www.xbiqugeo.com",
  "active": true
};

async function searchBook(keyword, pageNum) {
  function getBoolList($) {
    const bookArr = [];
    const liArr = $("ul.txt-list").eq(0).find('li');
    liArr.each(function (index, elem) {
      if (index !== 0) {
        let nameAndPath = $(this).find(".s2 a");
        bookArr.push({
          name: nameAndPath.html(),
          author: $(this).find(".s4").html(),
          path: nameAndPath.attr("href"),
          sourceId: source.id
        });
      }
    })
    return bookArr;
  }

  let url = source.searchBaseUrl + '/search?searchkey=' + encodeURIComponent(keyword);
  const html = await (await request(url)).text();
  const doc = load(html);
  const bookArr = getBoolList(doc);

  return {
    size: bookArr.length,
    pageNum: 1,
    pages: 1,
    sourceId: source.id,
    keyword: keyword,
    bookList: bookArr
  };
}

async function bookDetails(path) {
  const html = await (await request(source.bookBaseUrl + path)).text();
  let $ = load(html);

  return {
    name: $("meta[property='og:novel:book_name']").attr('content'),
    author: $("meta[property='og:novel:author']").attr('content'),
    path: path,
    sourceId: source.id,
    intro: $("meta[property='og:description']").attr('content'),
    coverUrl: $("meta[property='og:image']").attr('content'),
    type: $("meta[property='og:novel:category']").attr('content'),
  };
}

async function bookCatalog(path) {
  function getChapterInfo($) {
    let catalog = [];
    let chapters = $(".section-list").eq(1).find('li');
    chapters.each(function (i, elem) {
      const item = $(this);
      catalog.push({
        index: null,
        title: item.find('a').html(),
        path: item.find('a').attr("href")
      });
    });
    return catalog;
  }

  async function fetchAndParseChapterInfo(pageUrl) {
    return request(pageUrl)
      .then((response) => response.text())
      .then((html) => {
        return getChapterInfo(load(html));
      });
  }

  const html = await (await request(source.bookBaseUrl + path)).text();
  const $ = load(html);
  const chapterPage = $('select option');

  let urlArr = [];
  chapterPage.each(function (i, elem) {
    if (i !== 0)
      urlArr.push(source.bookBaseUrl + $(this).val())
  });
  //获取当前页面的章节
  let catalog = getChapterInfo($);

  //如果多页 多线程爬取
  if (urlArr.length > 0) {
    const promises = urlArr.map((pageUrl) => fetchAndParseChapterInfo(pageUrl));
    let chapterInfos = await Promise.all(promises)
    for (const iterator of chapterInfos) {
      catalog.push(...iterator);
    }
  }
  let index = 0;
  for (const iterator of catalog) {
    iterator.index = index++;
  }
  return catalog;
}

async function chapterContent(path) {
  const html = await (await request(source.bookBaseUrl + path)).text();
  const $ = load(html);
  //去除 非正文 去除网址
  $("#content .posterror").remove()
  $("#content a").remove()

  let content = '';
  const pTags = $("#content p");
  pTags.each(function (index) {
    content += $(this).text();
    // 如果不是最后一个p标签，则添加换行符
    if (index !== pTags.length - 1) {
      content += '\n';
    }
  });

  let next = $("#next_url")
  if (next.text().includes("下一页")) {
    content += "\n" + await chapterContent(next.attr("href"));
  }
  return content;
}

await testSearch("黎明", searchBook);

// await testContent("/book/18752/", { bookDetails, bookCatalog, chapterContent });
//debugger;

